package view.manager;

import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.DepartmentDao;
import dao.MajorDao;
import model.Department;
import model.Major;
import util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class MajorEditInterfrm extends JInternalFrame {
	private JTable table;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_3;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MajorEditInterfrm frame = new MajorEditInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MajorEditInterfrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("系部管理");
		setBounds(100, 100, 501, 556);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setEnabled(false);

		JLabel lblNewLabel = new JLabel("系部名称：");

		textField = new JTextField();
		textField.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Major> arrayList = new ArrayList<Major>();
				ArrayList<Major> List = new ArrayList<Major>();
				arrayList = MajorDao.select();
				for (Major major : arrayList) {
					if (major.getM_name().contains(textField.getText())) {
						List.add(major);
					}
				}
				filltable(List);
			}
		});
		btnNewButton.setIcon(new ImageIcon(MajorEditInterfrm.class.getResource("/images/select.png")));

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(46)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 105, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addContainerGap(47, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
					.addContainerGap())
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(36)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(57, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton))
					.addGap(43)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);

		JLabel lblNewLabel_1 = new JLabel("系部编号：");

		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("所属学院：");

		JLabel lblNewLabel_3 = new JLabel("系部名称：");

		textField_3 = new JTextField();
		textField_3.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("系部描述：");

		JTextArea textArea = new JTextArea();
		textArea.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要修改的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				Department department = (Department) comboBox.getSelectedItem();
				if (MajorDao.updata(new Major(textField_1.getText(), department.getD_id(), textField_3.getText(),
						textArea.getText())) > 0) {
					JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					filltable(MajorDao.select());
					return;
				}
				JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(MajorEditInterfrm.class.getResource("/images/updata.png")));

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要删除的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				int res = JOptionPane.showConfirmDialog(null, "是否确定删除");
				if (res == 0) {
					if (MajorDao.delete(textField_1.getText()) > 0) {
						JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						textField_1.setText("");
						textField_3.setText("");
						textArea.setText("");
						filltable(MajorDao.select());
						return;
					}
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(MajorEditInterfrm.class.getResource("/images/delete.png")));

		comboBox = new JComboBox();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel
								.createSequentialGroup().addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel
												.createSequentialGroup().addGap(27).addGroup(gl_panel
														.createParallelGroup(Alignment.LEADING)
														.addGroup(gl_panel.createSequentialGroup()
																.addComponent(lblNewLabel_3)
																.addPreferredGap(ComponentPlacement.RELATED)
																.addComponent(textField_3, GroupLayout.PREFERRED_SIZE,
																		156, GroupLayout.PREFERRED_SIZE))
														.addGroup(gl_panel.createSequentialGroup()
																.addComponent(lblNewLabel_1)
																.addPreferredGap(ComponentPlacement.RELATED)
																.addComponent(textField_1, GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(28).addComponent(lblNewLabel_2)
																.addPreferredGap(ComponentPlacement.RELATED)
																.addComponent(comboBox, 0, 121, Short.MAX_VALUE))))
										.addGroup(
												gl_panel.createSequentialGroup().addGap(28).addComponent(lblNewLabel_4))
										.addGroup(gl_panel.createSequentialGroup().addGap(30).addComponent(textArea,
												GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createSequentialGroup().addGap(86)
												.addComponent(btnNewButton_1).addGap(58).addComponent(btnNewButton_2)))
								.addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup().addContainerGap()
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
								.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_2).addComponent(
										comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup().addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(lblNewLabel_3).addGap(9).addComponent(lblNewLabel_4))
								.addGroup(gl_panel.createSequentialGroup().addGap(6).addComponent(textField_3,
										GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)))
						.addGap(5).addComponent(textArea, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(btnNewButton_1, Alignment.TRAILING)
								.addComponent(btnNewButton_2, Alignment.TRAILING))
						.addContainerGap()));
		panel.setLayout(gl_panel);

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				/**
				 * b表格点击事件
				 */
				comboBox.removeAllItems();
				int row = table.getSelectedRow();
				textField_1.setText(table.getValueAt(row, 0).toString());
				fillDepartmentType(table.getValueAt(row, 1).toString());
//				textField_2.setText(table.getValueAt(row, 1).toString());
				textField_3.setText(table.getValueAt(row, 2).toString());
				textArea.setText(table.getValueAt(row, 3).toString());
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
			},
			new String[] {
				"\u7CFB\u90E8\u7F16\u53F7", "\u6240\u5C5E\u5B66\u9662", "\u7CFB\u90E8\u540D\u79F0", "\u7CFB\u90E8\u63CF\u8FF0"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(61);
		table.getColumnModel().getColumn(1).setPreferredWidth(81);
		table.getColumnModel().getColumn(2).setPreferredWidth(131);
		table.getColumnModel().getColumn(3).setPreferredWidth(158);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);

		filltable(MajorDao.select());

	}

	private void filltable(ArrayList<Major> arrayList) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		for (Major major : arrayList) {
			Vector v = new Vector();
			v.add(major.getM_id());
			Department department = DepartmentDao.select(major.getD_id());
			v.add(department.getD_name());
			v.add(major.getM_name());
			v.add(major.getRemarks());
			dtm.addRow(v);
		}
	}

	/**
	 * 初始化下拉框
	 */
	private void fillDepartmentType(String firstComboBox) {
		ArrayList<Department> arrayList = DepartmentDao.select();
		for (Department department : arrayList) {
			if (firstComboBox.equals(department.getD_name())) {
				comboBox.addItem(department);
			}
		}
		for (Department department : arrayList) {
			if (!firstComboBox.equals(department.getD_name())) {
				comboBox.addItem(department);
			}
		}
	}
}
